namespace SilverSea.Untilities
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Windows.Forms;

    public static class PrintDGV
    {
        private static ArrayList arrayList_0 = new ArrayList();
        private static ArrayList arrayList_1 = new ArrayList();
        private static ArrayList arrayList_2 = new ArrayList();
        private static bool bool_0 = false;
        private static bool bool_1 = false;
        private static bool bool_2;
        private static bool bool_3 = true;
        private static Button button_0;
        private static CheckBox checkBox_0;
        private static ComboBox comboBox_0;
        public static string CompanyAddress = string.Empty;
        public static string CompanyName = string.Empty;
        private static DataGridView dataGridView_0;
        public static bool DisplayPrintOption = false;
        public static bool FitToPageWidth = false;
        public static int HeaderHeight = 40;
        private static int int_0 = 0;
        private static int int_1;
        private static int int_2;
        private static int int_3;
        private static int int_4;
        private static int int_5;
        public static bool IsLandscape = true;
        private static List<string> list_0 = new List<string>();
        private static List<string> list_1 = new List<string>();
        public static int marginsBottom = 50;
        public static int marginsLeft = 10;
        public static int marginsRight = 20;
        public static int marginsTop = 0x19;
        public static List<string> Notices = new List<string>();
        public static bool PrintDateTime = false;
        private static PrintDocument printDocument_0 = new PrintDocument();
        public static bool PrintNotice = false;
        public static string PrintTitle = string.Empty;
        public static string PrintTitleDetail1 = string.Empty;
        public static string PrintTitleDetail2 = string.Empty;
        private static string string_0 = "Ng\x00e0y .... th\x00e1ng .... năm ....";
        private static string string_1 = "Người kiểm tra";
        private static string string_2 = "Người duyệt";
        private static StringFormat stringFormat_0;
        private static StringFormat stringFormat_1;
        private static StringFormat stringFormat_2;

        public static void Print(DataGridView dataGridView)
        {
            try
            {
                dataGridView_0 = dataGridView;
                list_1.Clear();
                foreach (DataGridViewColumn column in dataGridView_0.Columns)
                {
                    if (column.Visible)
                    {
                        list_1.Add(column.HeaderText.Replace('\n', ' '));
                    }
                }
                PrintOptions options = new PrintOptions(list_1) {
                    FitToPageWidth = FitToPageWidth
                };
                if (DisplayPrintOption)
                {
                    if (options.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    FitToPageWidth = options.FitToPageWidth;
                    list_0 = options.GetSelectedColumns();
                }
                else
                {
                    list_0 = list_1;
                }
                PageSetupDialog dialog2 = new PageSetupDialog {
                    Document = printDocument_0
                };
                dialog2.PageSettings.Landscape = IsLandscape;
                dialog2.PageSettings.Margins.Top = marginsTop;
                dialog2.PageSettings.Margins.Left = marginsLeft;
                dialog2.PageSettings.Margins.Right = marginsRight;
                dialog2.PageSettings.Margins.Bottom = marginsBottom;
                if (options.ShowPageSettings)
                {
                    dialog2.ShowDialog();
                }
                printDocument_0.DefaultPageSettings = dialog2.PageSettings;
                bool_3 = true;
                int_5 = 0;
                PrintPreviewDialog dialog = new PrintPreviewDialog {
                    Document = printDocument_0,
                    ShowIcon = false
                };
                dialog.PrintPreviewControl.Zoom = 1.0;
                dialog.Width = Screen.PrimaryScreen.Bounds.Width;
                dialog.Height = Screen.PrimaryScreen.Bounds.Height;
                printDocument_0.BeginPrint += new PrintEventHandler(PrintDGV.smethod_0);
                printDocument_0.PrintPage += new PrintPageEventHandler(PrintDGV.smethod_1);
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    printDocument_0.BeginPrint -= new PrintEventHandler(PrintDGV.smethod_0);
                    printDocument_0.PrintPage -= new PrintPageEventHandler(PrintDGV.smethod_1);
                }
                else
                {
                    printDocument_0.Print();
                    printDocument_0.BeginPrint -= new PrintEventHandler(PrintDGV.smethod_0);
                    printDocument_0.PrintPage -= new PrintPageEventHandler(PrintDGV.smethod_1);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private static void smethod_0(object sender, PrintEventArgs e)
        {
            try
            {
                stringFormat_0 = new StringFormat();
                stringFormat_0.Alignment = StringAlignment.Near;
                stringFormat_0.LineAlignment = StringAlignment.Center;
                stringFormat_0.Trimming = StringTrimming.EllipsisCharacter;
                stringFormat_1 = new StringFormat();
                stringFormat_1.Alignment = StringAlignment.Center;
                stringFormat_1.LineAlignment = StringAlignment.Center;
                stringFormat_1.Trimming = StringTrimming.EllipsisCharacter;
                stringFormat_2 = new StringFormat();
                stringFormat_2.LineAlignment = StringAlignment.Center;
                stringFormat_2.FormatFlags = StringFormatFlags.NoWrap;
                stringFormat_2.Trimming = StringTrimming.EllipsisCharacter;
                arrayList_0.Clear();
                arrayList_1.Clear();
                arrayList_2.Clear();
                int_4 = 0;
                int_5 = 0;
                button_0 = new Button();
                checkBox_0 = new CheckBox();
                comboBox_0 = new ComboBox();
                int_1 = 0;
                foreach (DataGridViewColumn column in dataGridView_0.Columns)
                {
                    if (column.Visible && list_0.Contains(column.HeaderText.Replace('\n', ' ')))
                    {
                        int_1 += column.Width;
                    }
                }
                int_3 = 1;
                bool_2 = true;
                int_2 = 0;
                int_0 = 0;
                bool_0 = false;
                bool_1 = false;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private static void smethod_1(object sender, PrintPageEventArgs e)
        {
            int top = e.MarginBounds.Top;
            int left = e.MarginBounds.Left;
            Font font = new Font(dataGridView_0.Font, FontStyle.Regular);
            Font font2 = new Font(new Font("Arial", 14f), FontStyle.Bold);
            try
            {
                if (int_3 == 1)
                {
                    foreach (DataGridViewColumn column in dataGridView_0.Columns)
                    {
                        if (column.Visible && list_0.Contains(column.HeaderText.Replace('\n', ' ')))
                        {
                            int width;
                            if (FitToPageWidth)
                            {
                                width = (int) Math.Floor((double) (((((double) column.Width) / ((double) int_1)) * int_1) * (((double) e.MarginBounds.Width) / ((double) int_1))));
                            }
                            else
                            {
                                width = column.Width;
                            }
                            arrayList_0.Add(left);
                            arrayList_1.Add(width);
                            arrayList_2.Add(column.GetType());
                            left += width;
                        }
                    }
                }
                while (int_2 <= (dataGridView_0.Rows.Count - 1))
                {
                    DataGridViewRow row = dataGridView_0.Rows[int_2];
                    if (!row.IsNewRow && (bool_3 || row.Selected))
                    {
                        int num;
                        int_4 = row.Height;
                        if ((top + int_4) >= (e.MarginBounds.Height + e.MarginBounds.Top))
                        {
                            goto Label_0F8E;
                        }
                        if (bool_2)
                        {
                            float y = 10f;
                            if (CompanyName != string.Empty)
                            {
                                y += e.Graphics.MeasureString(CompanyName, font, e.MarginBounds.Width).Height;
                                e.Graphics.DrawString(CompanyName, font, Brushes.Black, (float) e.MarginBounds.Left, y);
                            }
                            if (CompanyAddress != string.Empty)
                            {
                                y += e.Graphics.MeasureString(CompanyAddress, font, e.MarginBounds.Width).Height;
                                e.Graphics.DrawString(CompanyAddress, font, Brushes.Black, (float) e.MarginBounds.Left, y);
                            }
                            StringFormat format = new StringFormat {
                                Trimming = StringTrimming.Word,
                                FormatFlags = StringFormatFlags.NoWrap,
                                Alignment = StringAlignment.Center
                            };
                            y += e.Graphics.MeasureString(PrintTitle, font2, e.MarginBounds.Width).Height + 10f;
                            RectangleF layoutRectangle = new RectangleF((float) e.MarginBounds.Left, y, (float) e.MarginBounds.Width, e.Graphics.MeasureString(PrintTitle, font2, e.MarginBounds.Width).Height);
                            e.Graphics.DrawString(PrintTitle, font2, Brushes.Black, layoutRectangle, format);
                            if (PrintTitleDetail1 != string.Empty)
                            {
                                y += e.Graphics.MeasureString(PrintTitleDetail1, font, e.MarginBounds.Width).Height + 10f;
                                e.Graphics.DrawString(PrintTitleDetail1, font, Brushes.Black, (float) e.MarginBounds.Left, y);
                            }
                            if (PrintTitleDetail2 != string.Empty)
                            {
                                y += e.Graphics.MeasureString(PrintTitleDetail2, font, e.MarginBounds.Width).Height;
                                e.Graphics.DrawString(PrintTitleDetail2, font, Brushes.Black, (float) e.MarginBounds.Left, y);
                            }
                            top = e.MarginBounds.Top + ((int) y);
                            num = 0;
                            foreach (DataGridViewColumn column2 in dataGridView_0.Columns)
                            {
                                if (column2.Visible && list_0.Contains(column2.HeaderText.Replace('\n', ' ')))
                                {
                                    e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), new Rectangle((int) arrayList_0[num], top, (int) arrayList_1[num], HeaderHeight));
                                    e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int) arrayList_0[num], top, (int) arrayList_1[num], HeaderHeight));
                                    e.Graphics.DrawString(column2.HeaderText, new Font(column2.InheritedStyle.Font, FontStyle.Bold), new SolidBrush(column2.InheritedStyle.ForeColor), new RectangleF((float) ((int) arrayList_0[num]), (float) top, (float) ((int) arrayList_1[num]), (float) HeaderHeight), stringFormat_1);
                                    num++;
                                }
                            }
                            bool_2 = false;
                            top += HeaderHeight;
                        }
                        num = 0;
                        bool flag = false;
                        foreach (DataGridViewCell cell in row.Cells)
                        {
                            if (cell.OwningColumn.Visible && list_0.Contains(cell.OwningColumn.HeaderText.Replace('\n', ' ')))
                            {
                                if ((((System.Type) arrayList_2[num]).Name != "DataGridViewTextBoxColumn") && (((System.Type) arrayList_2[num]).Name != "DataGridViewLinkColumn"))
                                {
                                    if (((System.Type) arrayList_2[num]).Name == "DataGridViewButtonColumn")
                                    {
                                        button_0.Text = cell.Value.ToString();
                                        button_0.Size = new Size((int) arrayList_1[num], int_4);
                                        Bitmap bitmap = new Bitmap(button_0.Width, button_0.Height);
                                        button_0.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
                                        e.Graphics.DrawImage(bitmap, new Point((int) arrayList_0[num], top));
                                    }
                                    else if (((System.Type) arrayList_2[num]).Name == "DataGridViewCheckBoxColumn")
                                    {
                                        checkBox_0.Size = new Size(14, 14);
                                        checkBox_0.Checked = Convert.ToBoolean(cell.Value.ToString());
                                        Bitmap image = new Bitmap((int) arrayList_1[num], int_4);
                                        Graphics.FromImage(image).FillRectangle(Brushes.White, new Rectangle(0, 0, image.Width, image.Height));
                                        checkBox_0.DrawToBitmap(image, new Rectangle((image.Width - checkBox_0.Width) / 2, (image.Height - checkBox_0.Height) / 2, checkBox_0.Width, checkBox_0.Height));
                                        e.Graphics.DrawImage(image, new Point((int) arrayList_0[num], top));
                                    }
                                    else if (((System.Type) arrayList_2[num]).Name == "DataGridViewComboBoxColumn")
                                    {
                                        comboBox_0.Size = new Size((int) arrayList_1[num], int_4);
                                        Bitmap bitmap3 = new Bitmap(comboBox_0.Width, comboBox_0.Height);
                                        comboBox_0.DrawToBitmap(bitmap3, new Rectangle(0, 0, bitmap3.Width, bitmap3.Height));
                                        e.Graphics.DrawImage(bitmap3, new Point((int) arrayList_0[num], top));
                                        e.Graphics.DrawString(cell.Value.ToString(), cell.InheritedStyle.Font, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF((float) (((int) arrayList_0[num]) + 1), (float) top, (float) (((int) arrayList_1[num]) - 0x10), (float) int_4), stringFormat_2);
                                    }
                                    else if (((System.Type) arrayList_2[num]).Name == "DataGridViewImageColumn")
                                    {
                                        Rectangle rectangle = new Rectangle((int) arrayList_0[num], top, (int) arrayList_1[num], int_4);
                                        Size size = ((Image) cell.FormattedValue).Size;
                                        e.Graphics.DrawImage((Image) cell.FormattedValue, new Rectangle(((int) arrayList_0[num]) + ((rectangle.Width - size.Width) / 2), top + ((rectangle.Height - size.Height) / 2), ((Image) cell.FormattedValue).Width, ((Image) cell.FormattedValue).Height));
                                    }
                                }
                                else
                                {
                                    DataGridViewColumn column3 = dataGridView_0.Columns[cell.ColumnIndex];
                                    e.Graphics.FillRectangle(new SolidBrush(column3.DefaultCellStyle.BackColor), new Rectangle((int) arrayList_0[num], top, (int) arrayList_1[num], int_4));
                                    if ((num == 0) && (cell.Value.ToString() == string.Empty))
                                    {
                                        flag = true;
                                    }
                                    if (flag)
                                    {
                                        e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), new Rectangle((int) arrayList_0[num], top, (int) arrayList_1[num], int_4));
                                    }
                                    e.Graphics.DrawString(" " + cell.Value.ToString(), cell.InheritedStyle.Font, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF((float) ((int) arrayList_0[num]), (float) top, (float) ((int) arrayList_1[num]), (float) int_4), stringFormat_0);
                                }
                                e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int) arrayList_0[num], top, (int) arrayList_1[num], int_4));
                                num++;
                            }
                        }
                        top += int_4;
                        int_2++;
                        if (int_3 == 1)
                        {
                            int_5++;
                        }
                    }
                    else
                    {
                        int_2++;
                    }
                }
                if (int_5 == 0)
                {
                    return;
                }
                if (!bool_1)
                {
                    smethod_2(e, int_5);
                    bool_1 = true;
                }
                if (PrintDateTime && !bool_0)
                {
                    if ((top + 60) >= (e.MarginBounds.Height + e.MarginBounds.Top))
                    {
                        e.HasMorePages = true;
                        return;
                    }
                    e.Graphics.DrawString(string_0, new Font(font, FontStyle.Italic), Brushes.Black, (float) ((e.MarginBounds.Left + (e.MarginBounds.Width / 2)) + 50), (float) (top + 15));
                    e.Graphics.DrawString(string_0, new Font(font, FontStyle.Italic), Brushes.Black, e.MarginBounds.Left + ((e.MarginBounds.Width - e.Graphics.MeasureString(string_0, dataGridView_0.Font, e.MarginBounds.Width).Width) - 50f), (float) (top + 15));
                    e.Graphics.DrawString(string_1, font, Brushes.Black, ((e.MarginBounds.Left + ((e.MarginBounds.Width + e.Graphics.MeasureString(string_0, font, e.MarginBounds.Width).Width) / 2f)) - (e.Graphics.MeasureString(string_1, font, e.MarginBounds.Width).Width / 2f)) + 50f, (float) (top + 30));
                    e.Graphics.DrawString(string_2, font, Brushes.Black, e.MarginBounds.Left + ((e.MarginBounds.Width - ((e.Graphics.MeasureString(string_0, dataGridView_0.Font, e.MarginBounds.Width).Width + e.Graphics.MeasureString(string_2, dataGridView_0.Font, e.MarginBounds.Width).Width) / 2f)) - 50f), (float) (top + 30));
                    bool_0 = true;
                }
                if (PrintNotice)
                {
                    for (top += 15; int_0 < Notices.Count; top += 15)
                    {
                        if (top >= (e.MarginBounds.Height + e.MarginBounds.Top))
                        {
                            goto Label_0F7C;
                        }
                        e.Graphics.DrawString(Notices[int_0], font, Brushes.Black, (float) e.MarginBounds.Left, (float) top);
                        int_0++;
                        if (Notices.Count > 3)
                        {
                            if (int_0 == Notices.Count)
                            {
                                break;
                            }
                            e.Graphics.DrawString(Notices[int_0], font, Brushes.Black, (float) (e.MarginBounds.Left + 250), (float) top);
                            int_0++;
                        }
                    }
                }
                goto Label_0F85;
            Label_0F7C:
                e.HasMorePages = true;
                return;
            Label_0F85:
                e.HasMorePages = false;
                return;
            Label_0F8E:
                smethod_2(e, int_5);
                bool_2 = true;
                int_3++;
                e.HasMorePages = true;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private static void smethod_2(PrintPageEventArgs printPageEventArgs_0, int int_6)
        {
            double count = 0.0;
            if (bool_3)
            {
                if (dataGridView_0.Rows[dataGridView_0.Rows.Count - 1].IsNewRow)
                {
                    count = dataGridView_0.Rows.Count - 2;
                }
                else
                {
                    count = dataGridView_0.Rows.Count - 1;
                }
            }
            else
            {
                count = dataGridView_0.SelectedRows.Count;
            }
            string s = "Page " + int_3.ToString() + " of " + Math.Ceiling((double) (count / ((double) int_6))).ToString();
            printPageEventArgs_0.Graphics.DrawString(s, dataGridView_0.Font, Brushes.Black, printPageEventArgs_0.MarginBounds.Left + ((printPageEventArgs_0.MarginBounds.Width - printPageEventArgs_0.Graphics.MeasureString(s, dataGridView_0.Font, printPageEventArgs_0.MarginBounds.Width).Width) / 2f), (float) (printPageEventArgs_0.MarginBounds.Top + printPageEventArgs_0.MarginBounds.Height));
        }
    }
}

